// components/camp-condition/camp-condition.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    campCondotionStatus: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    campCondotionStatus: false,
    //1:有水 2:有电 3:无水 4:无电 5:有新能源车充电桩 6:有卫生间 7:其他条件用
    conditionList: [ //营位条件数据列表
      {
        name: "有水",
        value: 1,
        state: 0
      },
      {
        name: "有电",
        value: 2,
        state: 0
      },
      {
        name: "无水",
        value: 3,
        state: 0
      },
      {
        name: "无电",
        value: 4,
        state: 0
      },
      {
        name: "有新能源车充电桩",
        value: 5,
        state: 0
      },
      {
        name: "有卫生间",
        value: 6,
        state: 0
      }
    ],
    currentData: [], //条件
    addList: [], //整体数据
    factorId: "", //所有条件id,
    memberPrice: '',
  },
  lifetimes: {
    ready() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //添加其他营卫条件
    addCampCondition() {
      let that = this;
      wx.showModal({
        title: '添加条件',
        editable: true,
        placeholderText: "请输入营位条件",
        success(res) {
          if (res.confirm) {
            that.data.conditionList.push({
              name: res.content,
              value: 7,
              state: 0
            })
            that.setData({
              conditionList: that.data.conditionList
            })
          }
        }
      })
    },
    //重置数据
    resetData() {
      const result = this.data.conditionList.filter(item => {
        item.state = 0;
        if (item.value != 7) {
          return item
        }
      })
      this.setData({
        conditionList: result,
        memberPrice: "",
        factorId: '',
        currentData: ''
      })
    },
    //添加虚拟数据
    addData(e) {
      const instance = getApp();
      const object = {
        memberPrice: Number(e.detail.value?.memberPrice) * 0.9,
        originalPrice: Number(e.detail.value.memberPrice),
        factorArr: this.data.currentData,
        factorId: this.data.factorId,
      }
      console.log(object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        instance.globalData.campData.campsiteData.push({
          memberPrice: Number(e.detail.value?.memberPrice) * 0.9,
          originalPrice: Number(e.detail.value.memberPrice),
          factor: this.data.currentData,
          factorId: this.data.factorId,
        });
        this.data.addList.push({
          memberPrice: Number(e.detail.value?.memberPrice) * 0.9,
          originalPrice: Number(e.detail.value.memberPrice),
          factorArr: this.data.currentData,
          factorId: this.data.factorId,
        })
        this.resetData();
        this.onClose();
        this.triggerEvent("sendData", this.data.addList)
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }

      // console.log(instance.globalData.campData)

    },
    //选中效果
    selectCondition(e) {
      let currentId = [];
      let ct = e.currentTarget.dataset.c
      const result = this.data.conditionList.map(item => {
        if (ct.name === item.name && ct.value === item.value) {
          item.state == 1 ? (item.state = 0) : (item.state = 1);
        }
        return item
      })
      const filterArr = result.filter(item => {
        if (item.state === 1) {
          return item
        }
      })
      result.forEach(item => {
        if (item.state === 1) {
          currentId.push(item.value)
        }
      })
      this.data.currentData = filterArr
      this.setData({
        factorId: currentId,
        conditionList: result,
        currentData: filterArr
      })

    },
    onClose() {
      this.resetData()
      this.setData({
        campCondotionStatus: false
      })
    },
  }
})